<?php

declare(strict_types=1);

namespace App\Controller;

use App\Repository\GroupUserRepository;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use Psr\Http\Message\ResponseInterface;

class GroupUserController
{
    protected ValidatorFactoryInterface $validate;

    protected GroupUserRepository $repository;

    public function __construct(ValidatorFactoryInterface $validate)
    {
        $this->validate = $validate;
        $this->repository = new GroupUserRepository();
    }

    public function index(RequestInterface $request)
    {

    }

    public function store(RequestInterface $request, $group_id): ResponseInterface
    {
        $data = $request->all();
        $validate = $this->validate->make($data, [
            'user_id' => 'required|integer|exists:users,id',
        ], customAttributes: [
            'user_id' => '用户'
        ]);

        if ($validate->fails())
            return response()->error(msg: $validate->errors()->first());

        $data['group_id'] = $group_id;

        return response()->success($this->repository->create($data));
    }
}
